/**
 * 断线重连菊花;
 */
import uiPanel from "./../../lib/UiPanel";
import { glConfig } from "../../Config/GLConfig";
const { ccclass, property } = cc._decorator;

@ccclass
class Reconnect extends uiPanel {
  //   @property(cc.Sprite)
  //   sp_reconnect: cc.Sprite = null;

  @property(cc.Label)
  lbl_reconnect: cc.Label = null;

  _interval_handler_lbl = null;
  _interval_connect_check = null;
  _loading_sec = 0;
  _reconnect_state = false;

  onLoad() {
    // this.init();
  }
  init() {
    //打开之后开始提示动画
    this.setData();
    this.setPage();
  }
  setData() {
    this._loading_sec = 0;
    this._reconnect_state = false;
  }
  setPage() {
    // circle
    // this.sp_reconnect.node.active = true;
    // var rotate = cc.rotateBy(1, -360);
    // var act = cc.repeatForever(rotate);
    // this.sp_reconnect.node.runAction(act);
    // lbl
    this.lbl_reconnect.node.active = true;
    var dotArr = {
      "0": ".",
      "1": "..",
      "2": "...",
      "3": "...."
    };
    var self = this;
    var fun = () => {
      if (self._loading_sec > 3) {
        self._loading_sec = 0;
      }
      //var str = cc.vv.GLocalizationDataModel.getStringByKey("Reconnect_Tips")
      var str = "网络不稳定,正在努力连接中";
      self.lbl_reconnect.string = str + dotArr[self._loading_sec];
      self._loading_sec++;
    };
    this._interval_handler_lbl = setInterval(fun, 500);

    if (!this._interval_connect_check) {
      this._interval_connect_check = setInterval(
        this.checkConnect.bind(this),
        1 / 24
      );
    }
  }
  checkConnect() {
    if (this._reconnect_state) {
      //避免二次进入;
      this.stopIntervalHandler();
      return;
    }
    var isConnected = cc.vv.GLConfig.isConnected;
    //如果连接上那么关闭界面
    if (isConnected) {
      this._reconnect_state = true;

      if (this._interval_connect_check) {
        this.stopIntervalHandler();
      }

      //关闭界面  不需要判断
      // var page = cc.vv.GLPageMgr.getPage("Page_Reconnect")
      // if (!page) {
      //     return;
      // }
      console.log("-------------checkConnect, to close page_reconnect   11!");
      //cc.vv.GLPageMgr.closePage("Page_Reconnect");
      cc.vv.UIMgr.close(cc.vv.GLConfig.reconnect_path, cc.vv.UIMgr.closeType.destory);
    } else {
      //  this.sp_reconnect.node.active = true;
      this.lbl_reconnect.node.active = true;
    }
  }
  onDisable() {
    this.stopIntervalHandler();
  }
  onDestroy() {
    this.stopIntervalHandler();
  }
  stopIntervalHandler() {
    console.log("____stopIntervalHandler__");
    if (this._interval_handler_lbl) {
      clearInterval(this._interval_handler_lbl);
      this._interval_handler_lbl = null;
    }
    if (this._interval_connect_check) {
      clearInterval(this._interval_connect_check);
      this._interval_connect_check = null;
    }
  }
}
